<template>
  <div>
    <el-form ref="formRef" :model="form" :rules="formRules" label-position="top">
      <el-form-item label="标题" prop="title" style="width: 50%">
        <el-input v-model="form.title" placeholder="请输入标题"></el-input>
      </el-form-item>

      <el-form-item label="正文" prop="content">
        <mavon-editor v-model="form.content" />
      </el-form-item>

      <el-form-item style="text-align: right">
        <el-button icon="el-icon-check" type="primary" @click="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  import { getAbout, updateAbout } from '@/api/about'

  export default {
    name: 'PersonalInfo',
    components: {},
    data() {
      return {
        form: {
          title: '',
          content: '',
        },
        formRules: {
          title: [{ required: true, message: '请输入标题', trigger: 'change' }],
        },
      }
    },
    created() {
      this.getData()
    },
    computed: {},
    methods: {
      getData() {
        getAbout().then((res) => {
          if (res.code === 200) {
            this.form.title = res.data.title
            this.form.musicId = res.data.musicId
            this.form.content = res.data.content
            this.form.commentEnabled = res.data.commentEnabled === 'true' ? true : false
          }
        })
      },
      submit() {
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            //纯数字
            const reg = /^\d{1,}$/
            if (!reg.test(this.form.musicId)) {
              return this.msgError('歌曲ID有误')
            }
            updateAbout(this.form).then((res) => {
              if (res.code === 200) {
                this.msgSuccess(res.msg)
              }
            })
          } else {
            return this.msgError('请填写必要的表单')
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped></style>
